<?php

/**
 * portal actions.
 *
 * @package    ems
 * @subpackage portal
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class portalActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      $this->portals = Doctrine_Core::getTable('EmsPortal')->getAllPortalsOrderByOrganization();
  }

  public function executeEditor(sfWebRequest $request)
  {
      $this->ems_portal = Doctrine::getTable('EmsPortal')->find(array($request->getParameter('id')));
      $this->forward404Unless($this->ems_portal);
  }

  public function executeShow(sfWebRequest $request)
  {
      /*$this->forward404Unless(
          $request->isXmlHttpRequest(),
          sprintf('Problema con el XmlHttpRequest haciendo AJAX para cargar los datos del portal (portal/show)')
      );*/
      
      $this->forward404Unless(
          $this->ems_portal = Doctrine::getTable('EmsPortal')->find(array($request->getParameter('id'))),
          sprintf('Object ems_portal does not exist (%s).', $request->getParameter('id'))
      );

      return $this->renderPartial('portal/show', array ('portal' => $this->ems_portal));
  }

  public function executeNew(sfWebRequest $request)
  {
      $this->form = new EmsPortalForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
      $this->forward404Unless($request->isMethod(sfRequest::POST));
      $this->form = new EmsPortalForm();
      $this->processForm($request, $this->form);
      $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless(
        $request->isXmlHttpRequest(),
        sprintf('Problema en el executeEdit.\n
            Metodo de petición esperado XmlHttpRequest.\n
            Recibido: '.$request->getMethod())
    );
    
    $this->forward404Unless(
        $this->ems_portal = Doctrine::getTable('EmsPortal')->find(array($request->getParameter('id'))),
        sprintf('Object ems_portal does not exist (%s).', $request->getParameter('id'))
    );
    
    $this->form = new EmsPortalForm($this->ems_portal);
    return $this->renderPartial('portal/form', array ('form' => $this->form));
    
  }

  public function executeUpdate(sfWebRequest $request)
  {
      $this->forward404Unless(
          $request->isXmlHttpRequest(),
          sprintf('Problema en el executeUpdate.\n
            Metodo de petición esperado XmlHttpRequest.\n
            Recibido: '.$request->getMethod())
      );
      $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
      $this->forward404Unless(
          $ems_portal = Doctrine::getTable('EmsPortal')->find(array($request->getParameter('id'))),
          sprintf('Object ems_portal does not exist (%s).', $request->getParameter('id'))
      );

      $this->form = new EmsPortalForm($ems_portal);

      if(!$this->processForm($request, $this->form)){
          return $this->renderPartial('portal/form', array ('form'=>$this->form));
      }
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($ems_portal = Doctrine::getTable('EmsPortal')
            ->find(array($request->getParameter('id'))),
            sprintf('Object ems_portal does not exist (%s).', $request->getParameter('id')));

    $ems_portal->delete();

    $this->redirect('portal/index');
  }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        if($request->isXmlHttpRequest()){
            $values = array ();
        } else {
            $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        }

        if ($form->isValid()){
            if($form->getObject()->isNew()){
                $ems_portal = $form->save();

                $section = new EmsSection();
                $section->setTitle('Home');
                $section->setEmsPortal($ems_portal);
                $section->setEmsCss(Doctrine::getTable('EmsCss')->findOneBy('id', 1));

                $section->save();
            } else {
                $ems_portal = $form->save();
            }
            if($request->isXmlHttpRequest()){
                return $this->renderPartial('portal/show', array('portal'=>$ems_portal));
            } else {
                $this->redirect('portal/editor?id='.$ems_portal->getId());
            }

        } else {
           return false;
        }
    }
}
